package com.appspot.didit.client.workitem;

import java.util.ArrayList;
import com.appspot.didit.client.BaseView;
import com.appspot.didit.client.tag.Tag;
import com.appspot.didit.client.tag.TagManager;
import com.appspot.didit.client.tag.TagService;
import com.appspot.didit.client.tag.TagServiceAsync;
import com.appspot.didit.client.user.LoginSuccessEvent;
import com.appspot.didit.client.user.LoginSuccessHandler;
import com.appspot.didit.client.user.UserManager;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class WorkitemManagePresenterImpl implements WorkitemManagePresenter {
	
	private final WorkitemServiceAsync workitemService = GWT.create(WorkitemService.class);
	
	//private final WorkitemCommandServiceAsync workitemCommandService = GWT.create(WorkitemCommandService.class);
	
	private final TagServiceAsync tagService = GWT.create(TagService.class);
	
	private final View view;

	private final CreateWorkitemPresenter cwPresenter;
	
	private final WorkitemListPresenter wlPresenter;	    	    
	
	@Inject  
	public WorkitemManagePresenterImpl(final HandlerManager eventBus, CreateWorkitemPresenter cwPresenter, WorkitemListPresenter wlPresenter, View view) {
	    	 	
		this.view = view;	    	
	    	
		this.cwPresenter = cwPresenter;	    	
	    	
		this.wlPresenter = wlPresenter;
		
		initUI();
		
		bindEvents(eventBus);	    
	}
	    
	private void bindEvents(final HandlerManager eventBus) {
	       
		eventBus.addHandler(LoginSuccessEvent.getType(), 
			new LoginSuccessHandler() {
				public void onLoginSuccess(LoginSuccessEvent event) {				
					LoadWorkitems(eventBus);
					LoadTags(eventBus);
				}	                        
			});
		
		/*eventBus.addHandler(LogoutEvent.getType(),	              
			new LogoutHandler() { 
			    public void onLogout(LogoutEvent event) {
			    	doLogout()
			    }	                
			});		*/
	}		
	
	@SuppressWarnings("unchecked")
	private void LoadWorkitems(final HandlerManager eventBus) {
		workitemService.loadWorkitems(UserManager.getInstance().getUserId(), new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to load workitems", caught);
            }
                    
            public void onSuccess(Object result) {
                GWT.log("Successfully loaded workitems", null);
                WorkitemManager.getInstance().setWorkitemList((ArrayList<Workitem>)result);
                eventBus.fireEvent(new WorkitemsLoadedEvent());
            }
		});	
		/*workitemCommandService.execute(new GetWorkitems(UserManager.getInstance().getUserId()), new GotWorkitems() {
			public void got(ArrayList<WorkitemDTO> workitems) {
				GWT.log("Successfully loaded workitems", null);
                WorkitemManager.getInstance().setWorkitemList(workitems);
                eventBus.fireEvent(new WorkitemsLoadedEvent());
			}
			
		});*/
	}
	
	@SuppressWarnings("unchecked")
	private void LoadTags(final HandlerManager eventBus) {
		final Long user_id = UserManager.getInstance().getUserId(); 
		tagService.loadTags(user_id, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				GWT.log("Failed to load tags", caught);
            }
                    
            public void onSuccess(Object result) {
                GWT.log("Successfully loaded tags", null);
                TagManager.getInstance().setTags((ArrayList<Tag>)result);
                eventBus.fireEvent(new TagsLoadedEvent());
            }
		});
		
	}
	
	private void initUI() {			
		view.addCreateWorkitem(cwPresenter.getView());
		view.addWorkitemList(wlPresenter.getView());
		/*userInfoPresenter.updateUserInfo();			
		view.showUserInfo(userInfoPresenter.getView());*/		
	}
	    
	public BaseView getView() {	           
		return view;	    
	}
		
	public View showWorkitemManage() {			
		return view;		
	}	
}